<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.teacherName" placeholder="教练姓名" clearable></el-input>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="审核通过" :value="1"></el-option>
                        <el-option label="审核拒绝" :value="2"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.teacher.rest.list" remoteSort :params="search" @selection-change="selectionChange" @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="账号" prop="account" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.teacher ? scope.row.teacher.account : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="教练姓名" prop="name" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.teacher ? scope.row.teacher.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="所属机构" prop="level" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.teacher ? scope.row.teacher.agent : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="手机号" prop="mobile" width="130" align="center">
                        <template #default="scope">
                             {{ scope.row.teacher ? scope.row.teacher.mobile : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="年龄" prop="age" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.teacher && scope.row.teacher.age ? scope.row.teacher.age : '-' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="性别" prop="age" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.teacher ? scope.row.teacher.gender : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="类型" prop="type" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.type.name || ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="申请时间段" prop="time" width="200" align="center">
                        <template #default="scope">
                            {{ scope.row.date }} {{ scope.row.startTime + '~' + scope.row.endTime }}
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="120" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="danger">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 2" type="warning">{{scope.row.status.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="提交时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="200">
                        <template #default="scope">
                            <el-popconfirm :title="'确认是否审核通过?'" @confirm="table_status(scope.row, 1)" v-if="permission.status && scope.row.status.value == 0">
                                <template #reference>
                                    <el-button type="text" size="small" v-if="permission.status && scope.row.status.value == 0">通过</el-button>
                                </template>
                            </el-popconfirm>
                            <el-divider direction="vertical" v-if="permission.status && scope.row.status.value == 0"></el-divider>
                            <el-popconfirm :title="'确认是否审核拒绝?'" @confirm="table_status(scope.row, 2)" v-if="permission.status && scope.row.status.value == 0">
                                <template #reference>
                                    <el-button type="text" size="small" v-if="permission.status && scope.row.status.value == 0">拒绝</el-button>
                                </template>
                            </el-popconfirm>
                            <el-divider direction="vertical" v-if="permission.delete && scope.row.status.value == 0"></el-divider>
                            <el-popconfirm :title="'确认是否要删除该申请记录?'" @confirm="table_del(scope.row)" v-if="permission.delete">
                                <template #reference>
                                    <el-button type="text" size="small" v-if="permission.delete">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

</template>

<script>
import { rolePermission } from '@/utils/permission'
export default {
    components: {
    
    },
    data() {
        return {
            selection: [],
            search: {
                teacherName: null,
            },
            permission: {
				delete: rolePermission('teacher-rest-delete'),
				status: rolePermission('teacher-rest-status')
			},
        }
    },
    mounted() {
        
    },
    methods: {
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //状态变更
        async table_status(row, status) {
            await this.$API.teacher.rest.updateStatus.put({ id: row.id, status: status }).then(() => {
                this.$notify.success("更新成功")
                this.$refs.table.refresh()
            })
        },
         //删除
         async table_del(row) {
            await this.$API.teacher.rest.action.delete({ id: row.id }).then(() => {
                this.$refs.table.refresh()
                this.$notify.success("删除成功")
            });
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
</style>
